<template>
	<view class="content">
		<u-datetime-picker :show="showStart" v-model="timeStamp" closeOnClickOverlay mode="date" @confirm="confirmStart"
			@cancel="showStart = false" @close="showStart = false">
		</u-datetime-picker>
		<mescroll-uni ref="mescrollRef" @init="mescrollInit" @up="upCallback" :up="upOption" :down="{use: false}">
			<view class="section-0">
				<view class="section-body">
					<view class="item" v-for="item in board" :key="item.name">
						<view class="shape" v-if="item.right"></view>
						<view class="top">{{item.name}}</view>
						<view class="bottom">{{item.count}}</view>
					</view>
				</view>
			</view>
			<view class="section-1">
				<view class="form">
					<view class="form-item">
						<view class="label">项目名称</view>
						<view class="shape"></view>
						<view class="input">
							<u--input placeholderClass="input-placeholder" class="input-body"
								v-model="form.projectName"></u--input>
						</view>
					</view>
					<view class="form-item">
						<view class="label">时间</view>
						<view class="shape"></view>
						<view class="input">
							<div class="time-body" @click="showDate('start_time')">{{form.start_time}}</div>
							<div class="time-split">-</div>
							<div class="time-body" @click="showDate('end_time')">{{form.end_time}}</div>
						</view>
					</view>
					<view class="actions">
						<view class="action" @click="handleResetForm">
							<text>重</text>
							<text>置</text>
						</view>
						<view class="action search" @click="handleSearch">
							<text>查</text>
							<text>询</text>
						</view>
					</view>
				</view>
				<view class="notice">
					<view class="notice-button" @click="handleRemin('1', false)">一键通知 <text
							class="iconfont icon-bell"></text></view>
				</view>
				<view class="list">
					<view class="item" v-for="(item, index) in list" :key="item.id + index">
						<view class="parent">
							<view class="action" @click="expandChange(item.id)">
								<view class="iconfont icon-xiangyou1"></view>
							</view>
							<view class="name">{{index + 1}}.{{item.project_name}}</view>
							<view class="action2" @click="handleRemin('2', item.id)">
								<view class="iconfont icon-bell"></view>
							</view>
						</view>

						<view class="child" v-if="expandList.indexOf(item.id) !== -1">
							<view class="child-item" @click="go(item2)" v-for="(item2, idx) in item.children"
								:key="item2.id + idx">
								<view class="top">
									<view class="name">
										{{index + 1}}-{{idx + 1}}.{{item2.project_name}}
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-uni>
	</view>
</template>

<script>
	let doExport = require('@/src/utils/exportExcel.js');
	import {
		reportListApi,
		reportRemindApi
	} from '@/api/statistics.js';
	import {
		mapGetters,
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				showStart: false,
				timeStamp: '',

				name: '',
				board: [{
						name: '项目总数',
						count: 0,
						right: true
					},
					{
						name: '未提交项目总数',
						count: 0
					},
				],
				list: [],
				expandList: [],
				form: {
					projectName: '',
					start_time: '',
					end_time: ''
				},
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				upOption: {
					textNoMore: '-- 没有更多了 --'
				},
				pageSize: 20,
				lock: false
			};
		},
		methods: {
			handleExport() {
				let name = '履职记录'
				let arr = [
					[{
						content: '项目',
						type: 'text',
						width: '200px',
						height: '25px',
						fontSize: '16px'
					}],
				];
				this.list.forEach(item => {
					let _arr = [{
						content: item.project_name,
						type: 'text',
						width: '400px',
						height: '40px',
						fontSize: '13px'
					}, ]
					arr.push(_arr);
				})
				doExport(name, arr, (res) => {
					uni.openDocument({
						filePath: '/storage/emulated/0' + res,
						success: function(res) {}
					});
				})
			},
			go(params) {
				uni.navigateTo({
					url: '/pages/statistics/progress/week/info/index?params=' + JSON.stringify(params)
				})
			},
			expandChange(id, project_id) {
				const idx = this.expandList.indexOf(id);
				if (idx !== -1) {
					this.expandList.splice(idx, 1);
				} else {
					this.expandList.push(id);
				}

			},
			async handleRemin(type, project_id) {
				if (this.lock) {
					return;
				}
				uni.showLoading({
					title: '加载中...'
				});
				this.lock = true;
				let obj = {
					type
				}
				if (project_id) {
					obj.project_id = project_id;
				}
				const {
					data
				} = await reportRemindApi(obj);
				uni.hideLoading();
				this.lock = false;
				if (data.code === 1) {
					uni.showToast({
						title: '提醒成功',
						duration: 1000,
						icon: 'success'
					});
				} else {
					uni.showToast({
						title: '提醒失败',
						duration: 1000,
						icon: 'error'
					});
				}
			},
			handleResetForm() {
				for (let key in this.form) {
					this.form[key] = '';
				}
				this.mescroll.resetUpScroll();
				this.mescroll.scrollTo(0, 0);
			},
			handleSearch() {
				this.mescroll.resetUpScroll();
				this.mescroll.scrollTo(0, 0);
			},
			showDate(type) {
				this.timeType = type;
				this.showStart = true;
			},
			closeDate(type) {
				this.timeType = '';
				this.showStart = false;
			},
			formatDate(timeStamp) {
				const date = new Date(timeStamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
				const Y = date.getFullYear() + '-';
				const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				const D = date.getDate()<10? '0' + date.getDate() : date.getDate();
				const h = date.getHours() + ':';
				const m = date.getMinutes() + ':';
				const s = date.getSeconds();
				return Y + M + D;
			},
			confirmStart(val) {
				const timeStamp = val.value;
				this.form[this.timeType] = this.formatDate(timeStamp);
				this.closeDate();
			},
			/*mescroll组件初始化的回调,可获取到mescroll对象 (此处可删,mixins已默认)*/
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*上拉加载的回调*/
			async upCallback(page) {
				const {
					data
				} = await reportListApi({
					...this.form,
					page: page.num,
					pageSize: this.pageSize
				});

				if (data.code === 1 || data.code === 200) {
					let arr = [];
					for (let key in data.data.data) {
						arr.push(data.data.data[key]);
					}
					// 接口返回的当前页数据列表 (数组)
					let curPageData = arr;
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length;
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					let totalPage = data.data.data.last_page;
					//设置列表数据
					if (page.num == 1) this.list = []; //如果是第一页需手动置空列表
					this.list = this.list.concat(curPageData); //追加新数据
					// 请求成功,隐藏加载状态
					this.mescroll.endByPage(curPageLen, totalPage);
					setTimeout(() => {
						this.mescroll.endSuccess(curPageLen);
					}, 20);
				} else {
					//  请求失败,隐藏加载状态
					this.mescroll.endErr()
				}
			},
			// async fetchList() {
			// 	uni.showLoading({title: '加载中...'});
			// 	const { data } = await reportListApi({
			// 		...this.form
			// 	});
			// 	uni.hideLoading();
			// 	if (data.code === 1 || data.code === 200) {
			// 		this.list = data.data.projectList;
			// 		this.board[0].count = data.data.ytjCount;
			// 		this.board[1].count = data.data.wtjCount;
			// 	} 
			// },
		},
		onReady() {
			// this.fetchList();
			this.timeStamp = (new Date()).valueOf();
		},
		onNavigationBarButtonTap(res) {
			this.handleExport()
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #fff;
	}

	.section-1 {
		position: relative;
		top: -50rpx;
		z-index: 10;
		background-color: #fff;
		border-top-left-radius: 50rpx;
		border-top-right-radius: 50rpx;

		.notice {
			width: 100%;
			@include clear();
			margin-bottom: 20rpx;

			.notice-button {
				margin-right: 40rpx;
				float: right;
				width: 270rpx;
				height: 79rpx;
				background-color: #ffffff;
				border-radius: 35rpx;
				box-shadow: 0 0 30rpx 8rpx rgba(#bcbcb5, 0.17);
				text-align: center;
				line-height: 79rpx;
				font-size: 26rpx;
				font-weight: normal;
				font-stretch: normal;
				letter-spacing: 0rpx;
				color: #46464a;
				font-weight: bold;

				.iconfont {
					font-size: 32rpx;
					color: #ffde00;
					font-size: 32rpx;
					margin-left: 20rpx;
				}
			}
		}

		.list {
			width: 670rpx;
			box-sizing: border-box;
			margin-left: 40rpx;

			.item {
				width: 100%;
				border-bottom: solid 2rpx #f2f2f0;
				font-size: 26rpx;
				@include clear();
				box-sizing: border-box;

				.parent {
					width: 100%;
					@include clear();

					.name {
						float: left;
						width: 520rpx;
						line-height: 30rpx;
						padding: 26rpx 0;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						text-overflow: ellipsis;
						overflow: hidden;
						white-space: nowrap;
						letter-spacing: 2rpx;
						color: #3b3c40;
					}

					.action {
						float: left;
						width: 32rpx;
						margin-right: 18rpx;
						line-height: 82rpx;
						text-align: center;
						color: #3b3c40;

						.iconfont {
							font-size: 32rpx;
							transition: 0.2s ease;
							transform: rotate(90deg);
						}

					}

					.action2 {
						float: right;
						font-size: 32rpx;
						color: #ffde00;
						line-height: 82rpx;
					}
				}

				.child {
					width: 750rpx;
					margin-left: - 40rpx;
					background: #f2f2fa;

					.child-item {
						width: 100%;
						border-bottom: 2rpx solid #e3e8f5;
						@include clear();

						&:last-child {
							border-bottom: none;
						}


						.top {
							width: 100%;
							overflow: hidden;
							box-sizing: border-box;
							padding-left: 90rpx;

							.name {
								float: left;
								width: calc(100% - 90rpx);
								box-sizing: border-box;
								padding: 26rpx 0;
								font-size: 26rpx;
								color: #3b3c40;
								font-size: 26rpx;
							}

							.action {
								width: 32rpx;
								line-height: 82rpx;
								float: left;
								text-align: center;
								color: #7f98ae;
								margin-right: 18rpx;

								.iconfont {
									font-size: 32rpx;
									transition: 0.2s ease;
									transform: rotate(90deg);
								}
							}
						}
					}
				}

			}
		}

		.actions {
			width: 100%;
			height: 50rpx;
			margin-top: 29rpx;
			padding-bottom: 40rpx;
			display: flex;
			justify-content: space-between;

			.action {
				width: 288rpx;
				height: 48rpx;
				background-color: #395cf5;
				border-radius: 20rpx;
				text-align: center;
				line-height: 48rpx;
				font-size: 26rpx;
				font-weight: normal;
				font-stretch: normal;
				color: #ffffff;

				text {
					&:first-child {
						margin-right: 20rpx;
					}
				}
			}

			.search {
				background-color: #1534ce;
			}
		}

		.form {
			width: 100%;
			padding: 0 75rpx;
			padding-top: 40rpx;
			box-sizing: border-box;

			.form-item {
				width: 600rpx;
				height: 48rpx;
				border-radius: 20rpx;
				border: solid 2rpx #1534ce;
				margin: 0 auto;
				margin-bottom: 34rpx;
				@include clear();

				.label {
					float: left;
					width: 134rpx;
					height: 48rpx;
					text-align: center;
					line-height: 44rpx;
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #1534ce;
				}

				.shape {
					width: 3rpx;
					height: 32rpx;
					background-color: #7f98ae;
					float: left;
					margin-top: 8rpx;
				}

				.input {
					float: left;
					width: calc(100% - 134rpx - 3rpx);

					.time-body {
						height: 48rpx;
						width: 45%;
						float: left;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.7);
						text-align: center;
						line-height: 48rpx;
					}

					.time-split {
						float: left;
						width: 10%;
						text-align: center;
						color: #1534ce;
						line-height: 44rpx;
					}

					.input-body {
						box-sizing: border-box;
						width: 100%;
						height: 44rpx;
						padding: 0 !important;
						border: none;
						padding-left: 30rpx !important;
						padding-right: 30rpx !important;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.7);
					}

					.input-placeholder {
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.5);
					}
				}
			}

			.size2 {
				.label {
					width: 77rpx;
				}

				.input {
					width: calc(100% - 77rpx - 3rpx);
					position: relative;

					.iconfont {
						position: absolute;
						right: 25rpx;
						top: 7rpx;
						font-size: 30rpx;
						color: #dad9df;
					}
				}
			}
		}
	}

	.section-0 {
		width: 100%;
		background-color: #2e5efe;

		.section-body {
			width: 100%;
			padding-left: 40rpx;
			height: 230rpx;
			padding-right: 40rpx;
			padding-bottom: 50rpx;
			background-color: #1534ce;
			background-size: cover;
			background-repeat: no-repeat;
			border-top-left-radius: 50rpx;
			border-top-right-radius: 50rpx;
			overflow: hidden;
			box-sizing: border-box;

			.item {
				width: 50%;
				float: left;
				display: flex;
				flex-wrap: wrap;
				align-content: space-between;
				padding-top: 40rpx;
				height: 100rpx;
				position: relative;

				.shape {
					width: 4rpx;
					height: 84rpx;
					background-image: linear-gradient(rgba(#3658f2, 0.3) 0%, #3658f2 30%, #3658f2 70%, rgba(#3658f2, 0.3) 100%, );
					position: absolute;
					right: 0;
					top: 48rpx;
				}

				.top {
					width: 100%;
					text-align: center;
					font-size: 16rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #95c4fa;
				}

				.bottom {
					width: 100%;
					text-align: center;
					font-size: 58rpx;
					font-weight: bold;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #ffde00;
				}
			}
		}
	}
</style>
